<template>
   <div class="box">
    <h1>添加</h1>
    <div class="add_box">
        <div class="modal">
            <table class="editTB">
                <tr>
                    <td>标题：</td>
                    <td>
                        <input type="text" name="" id="" v-model="blog.title">
                    </td>
                </tr>
                <tr>
                    <td>作者：</td>
                    <td>
                        <input type="text" name="" id="" v-model="blog.author">
                    </td>
                </tr>
                <tr>
                    <td>类别：</td>
                    <td>
                        <input type="text" name="" id="" v-model="blog.flag">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="保存" @click="save(blog.id)">
                        <input type="button" value="取消" @click="cancel()">
                    </td>
                </tr>
            </table>
        </div>
    </div>
  </div> 
</template>
<script setup>
    import {reactive} from 'vue';
    import {useRouter} from 'vue-router';
    import axios from 'axios';
    const blogs = reactive([]);
    const router = useRouter();
    const blog = reactive({
        id: 0,
        title: '',
        author: '',
        flag: ''
    });
    async function save(){
    let obj ={
      title:blog.title,
      author:blog.author,
      flag:blog.flag
    }
      let res = await axios.post('http://localhost:3000/blog', obj);
      if (res.data.code === 1000) {
        blogs.push(res.data.data);
        router.push('/app');
      }
    
  }
  function cancel(){
    router.push('/app');
  }
</script>
<style scoped>
       .box{
          background-color: white;
          margin: 0;
          padding: 0;
          display: flex;
          align-items: center;
          height: 100vh;
          flex-direction: column;
        }
        

        .add-box {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 20px;
            max-width: 400px;
            width: 100%;
        }

        .add-box h1 {
            font-size: 24px;
            margin-bottom: 20px;
            text-align: center;
        }

        .editTB {
            width: 100%;
        }

        .editTB td {
            padding: 8px 0;
        }

        .editTB input[type="text"] {
            width: calc(100% - 10px);
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            margin-top: 4px;
        }

        .editTB input[type="button"] {
            padding: 8px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            background-color: #007bff;
            color: #fff;
        }

        .editTB input[type="button"]:hover {
            background-color: #0056b3;
        }

        .editTB input[type="button"]:last-child {
            margin-left: 10px;
            background-color: #6c757d;
        }

        .editTB input[type="button"]:last-child:hover {
            background-color: #5a6268;
        }
</style>